package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.blankj.utilcode.util.ApiUtils;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 加油车首页订单列表
 */
public class OilHomeOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<OilHomeBean> oilOrderBeanList;
    private Context context;
    private RecyclerViewItemClickLisenter<OilHomeBean> oilHomeBeanRecyclerViewItemClickLisenter;
    private OilOrderHanldeCallBack oilOrderHanldeCallBack;
    java.text.DecimalFormat df =new java.text.DecimalFormat("#.###");

    public void setOilOrderHanldeCallBack(OilOrderHanldeCallBack oilOrderHanldeCallBack) {
        this.oilOrderHanldeCallBack = oilOrderHanldeCallBack;
    }

    public void setOilHomeBeanRecyclerViewItemClickLisenter(RecyclerViewItemClickLisenter<OilHomeBean> oilHomeBeanRecyclerViewItemClickLisenter) {
        this.oilHomeBeanRecyclerViewItemClickLisenter = oilHomeBeanRecyclerViewItemClickLisenter;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            OilHomeBean oilHomeBean = (OilHomeBean) view.getTag();
            if (view.getId() == R.id.tv_arrive) {
                oilOrderHanldeCallBack.handle(oilHomeBean);
            } else {
                oilHomeBeanRecyclerViewItemClickLisenter.onItemClickLisenter(oilHomeBean);
            }
        }
    };

    public OilHomeOrderAdapter(List<OilHomeBean> oilOrderBeanList, Context context) {
        this.oilOrderBeanList = oilOrderBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_oil_home, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder = (MyHolder) holder;
        OilHomeBean oilOrderBean = oilOrderBeanList.get(position);
        myHolder.tvNumber.setText(oilOrderBean.getMeshNum());
        myHolder.tvTime.setText(oilOrderBean.getSendTime());
        myHolder.tvBianhao.setText(oilOrderBean.getOrderNum());
        myHolder.tvType.setText(oilOrderBean.getMeshTypeName());
        myHolder.tvConcatr.setText(oilOrderBean.getSenderName());
        myHolder.tvPhone.setText(oilOrderBean.getSenderPhone());
        myHolder.tvPhone.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        myHolder.tvPhone.getPaint().setAntiAlias(true);//抗锯齿
        if (!TextUtils.isEmpty(oilOrderBean.getMeshLatitude())){
            double lat=Double.parseDouble(oilOrderBean.getMeshLatitude());
            double lnt=Double.parseDouble(oilOrderBean.getMeshLongitude());
            LatLng latLng2=new LatLng(lat,lnt);
            LatLng latLng=new LatLng(LocationManager.getInstance().getLatitude(),LocationManager.getInstance().getLongitude());
            try {
                String ds=df.format(AMapUtils.calculateLineDistance(latLng,latLng2)/1000);
                if (ds.contains("-")){
                    myHolder.tvJl.setText("0.01KM");

                }else {
                    myHolder.tvJl.setText(ds+"KM");
                }
            }catch (Exception e){
                myHolder.tvJl.setText("0.01KM");

            }
        }else {
            myHolder.tvJl.setText("");
        }
        if (!TextUtils.isEmpty(oilOrderBean.getMeshLatitude())){
            double lat=Double.parseDouble(oilOrderBean.getMeshLatitude());
            double lnt=Double.parseDouble(oilOrderBean.getMeshLongitude());
            LatLng latLng2=new LatLng(lat,lnt);
            LatLng latLng=new LatLng(LocationManager.getInstance().getLatitude(),LocationManager.getInstance().getLongitude());
            String str=AMapUtils.calculateLineDistance(latLng,latLng2)/1000+"";
            if (str.contains("-")){
                myHolder.tvJl.setText("0.0KM");

            }else {
                myHolder.tvJl.setText(str+"KM");
            }        }else {
            myHolder.tvJl.setText("");
        }

        if (!TextUtils.isEmpty(oilOrderBean.getDistance())){
            myHolder.tvJl.setText(oilOrderBean.getDistance()+"KM");
        }
        if (TextUtils.isEmpty(oilOrderBean.getMeshAddress())) {
            if (!TextUtils.isEmpty(oilOrderBean.getMeshLatitude())) {
                double lat=Double.parseDouble(oilOrderBean.getMeshLatitude());
                double lnt=Double.parseDouble(oilOrderBean.getMeshLongitude());
                GeocodeSearch geocoderSearch = new GeocodeSearch(context);
                geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {

                    @Override
                    public void onGeocodeSearched(GeocodeResult result, int rCode) {


                    }

                    @Override
                    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
                        try {
                            myHolder.tvLocation.setText(result.getRegeocodeAddress().getFormatAddress());
                        } catch (Exception e) {

                        }
                    }
                });
                com.amap.api.services.core.LatLonPoint lp = new com.amap.api.services.core.LatLonPoint(lat, lnt);
                RegeocodeQuery query = new RegeocodeQuery(lp, 200, GeocodeSearch.AMAP);
                geocoderSearch.getFromLocationAsyn(query);

            }
        } else {
            myHolder.tvLocation.setText(oilOrderBean.getMeshAddress());
        }
        myHolder.tvMoney.setText(oilOrderBean.getExpectMeter() + "L");
        if (oilOrderBean.isPayed()) {
            myHolder.tvArrive.setText("已完成");
            myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_login_btn));
        } else {
            if ("waiting".equals(oilOrderBean.getOrderState())) {
                myHolder.tvArrive.setText("待确认");
                myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_login_btn));
            } else if ("confirmed".equals(oilOrderBean.getOrderState())) {
                myHolder.tvArrive.setText("确认到达");
                myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_login_btn));
            } else if ("arrived".equals(oilOrderBean.getOrderState())) {
                myHolder.tvArrive.setText("完成加油");
                myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_login_btn));
            } else if ("finished".equals(oilOrderBean.getOrderState())) {
                if (oilOrderBean.getPaymentType()==5){
                    myHolder.tvArrive.setText("确认收款");
                    myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_login_btn));
                }else {
                    myHolder.tvArrive.setText("等待支付");
                    myHolder.tvArrive.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_dd));
                }
            }
        }
        myHolder.tvArrive.setTag(oilOrderBean);
        myHolder.tvArrive.setOnClickListener(onClickListener);
        myHolder.itemView.setTag(oilOrderBean);
        myHolder.itemView.setOnClickListener(onClickListener);
    }

    @Override
    public int getItemCount() {
        return oilOrderBeanList.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_number_tv)
        TextView tvNumber;
        @BindView(R.id.item_time_tv)
        TextView tvTime;
        @BindView(R.id.item_bianhao_tv)
        TextView tvBianhao;
        @BindView(R.id.item_type_tv)
        TextView tvType;
        @BindView(R.id.item_concat_tv)
        TextView tvConcatr;
        @BindView(R.id.item_phone_tv)
        TextView tvPhone;
        @BindView(R.id.item_juli_tv)
        TextView tvJl;
        @BindView(R.id.item_location_tv)
        TextView tvLocation;
        @BindView(R.id.item_money_tv)
        TextView tvMoney;
        @BindView(R.id.tv_arrive)
        TextView tvArrive;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OilOrderHanldeCallBack {
        void handle(OilHomeBean oilHomeBean);
    }
}
